import { ref } from 'vue'

export const useLoading = () => {
  const initState = ref<boolean>(false)

  // 开始loading
  const start = () => {
    initState.value = true
  }

  // 开始loading
  const stop = () => {
    initState.value = false
  }

  const promise = <T>(promise: Promise<T>) => {
    start()
    return promise.finally(stop)
  }

  return {
    state: initState,
    start,
    stop,
    promise,
  }
}
